<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="使用登记证编号" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.license`" class="mb-0px!">
            <el-input v-model="row.license" placeholder="请输入使用登记证编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" min-width="100">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.used`" class="mb-0px!">
            <el-switch
              v-model="row.used"
              :active-value="1"
              :inactive-value="2"
              size="default"
              style="--el-switch-on-color: #13ce66"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="下次检验日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.nextCheckTime`" class="mb-0px!">
            <el-date-picker
              type="date"
              value-format="x"
              v-model="row.nextCheckTime"
              placeholder="请选择下次检验日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备品种" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.equipType`"
            :rules="[{ required: row.equipType, message: '设备品种不能为空', trigger: 'blur' }]"
            class="mb-0px!"
          >
            <el-select v-model="row.equipType" placeholder="请选择设备型号">
              <el-option
                v-for="item in getIntDictOptions(DICT_TYPE.CRM_SPECIAL_EQUIPMENT_TYPE)"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.equipModel`" class="mb-0px!">
            <el-input v-model="row.equipModel" placeholder="请输入设备品种" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.equipName`"
            :rules="[{ required: row.equipName, message: '设备名称不能为空', trigger: 'blur' }]"
            class="mb-0px!"
          >
            <el-input v-model="row.equipName" placeholder="请输入设备名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="设备使用地址" min-width="500">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.location`" class="mb-0px!">
            <el-input v-model="row.location" placeholder="请输入设备使用地址" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="安全管理人员" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.safer`"
            :rules="[{ required: row.safer, message: '安全管理人员不能为空', trigger: 'blur' }]"
            class="mb-0px!"
          >
            <el-input v-model="row.safer" placeholder="请输入安全管理人员" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="安全管理员电话" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.safemobile`"
            :rules="[
              { required: row.safemobile, message: '安全管理员电话不能为空', trigger: 'blur' }
            ]"
            class="mb-0px!"
          >
            <el-input v-model="row.safemobile" placeholder="请输入安全管理员电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="登记日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.registerTime`" class="mb-0px!">
            <el-date-picker
              type="date"
              value-format="x"
              v-model="row.registerTime"
              placeholder="请输入登记日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="发证日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.licenseTime`" class="mb-0px!">
            <el-date-picker
              type="date"
              value-format="x"
              v-model="row.licenseTime"
              placeholder="请输入发证日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="检验责任主体" min-width="300">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.unit`" class="mb-0px!">
            <el-input v-model="row.unit" placeholder="请输入检验责任主体" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="检验结论" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.result`" class="mb-0px!">
            <el-input v-model="row.result" placeholder="请输入检验结论" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link circle
            ><Icon icon="ep:remove" color="red"
          /></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button type="primary" plain @click="handleAdd" round
      ><Icon icon="ep:plus" class="mr-2" />增加行</el-button
    >
  </el-row>
</template>
<script setup lang="ts">
import { ComplianceApi } from '@/api/crm/compliance'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const props = defineProps<{
  complianceId: undefined // 合规性编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  complianceId: [{ required: true, message: '合规性编号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.complianceId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await ComplianceApi.getComplianceSpecialEquipmentListByComplianceId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    complianceId: undefined,
    license: undefined,
    used: undefined,
    nextCheckTime: undefined,
    equipType: undefined,
    equipName: undefined,
    equipModel: undefined,
    registerTime: undefined,
    location: undefined,
    safer: undefined,
    safemobile: undefined,
    licenseTime: undefined,
    unit: undefined,
    result: undefined
  }
  row.complianceId = props.complianceId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
